home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7184 < prev    next >
Encoding:
Text File  |  1996-08-05  |  7.2 KB  |  171 lines

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.os.msdos.programmer,comp.lang.c
  4. Subject: Re: open vs fopen?
  5. Date: 17 Feb 96 02:59:24 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.824525964@rscernix>
  8. References: <uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8bev$6tr@hermes.louisville.edu> <2d3avbl60.alamito@marketgraph.xs4all.nl> <4ftusv$181@newshost.cyberramp.net> <danpop.824430285@rscernix> <4g39d4$ej8@newshost.cyberramp.net>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4g39d4$ej8@newshost.cyberramp.net> sinan@cyberramp.net (John Noland) writes:
  13.  
  14. >In article <danpop.824430285@rscernix>, danpop@mail.cern.ch says...
  15. >>
  16. >>In <4ftusv$181@newshost.cyberramp.net> sinan@cyberramp.net (John Noland) writes:
  17. >>
  18. >>>The open() function and its ilk are normally referred to as the "low-level"
  19. >>>I/O package. fopen() is the "Buffered" or "Standard" I/O package. The 
  20. >>>strength of the low-level I/O functions is that they offer excellent control,
  21. >>>particularly when used with binary files.
  22. >>
  23. >>???  What can read/write do on a binary file that fread/fwrite cannot do?
  24. >
  25. >Where in the above did I compare them with each other? 
  26.  
  27. In the above paragraph.  The "low-level I/O functions" means essentially
  28. read/write.
  29. >>
  30. >>>If you have a special I/O need, you
  31. >>>can use the low-level I/O routines to fashion the exact I/O package to fit
  32. >>>your needs. 
  33. >>
  34. >>Same question as above.
  35. >
  36. >Same question from me again. Are the read/write functions written using fread/
  37. >fwrite or is it vice versa? I believe, and feel free to correct me if I'm 
  38. >wrong, that the fread/fwrite functions are written using the read/write 
  39. >functions.
  40.  
  41. True (in Unix, probably false on other platforms) but irrelevant.  There
  42. are no I/O needs that can be satisfied by one set of functions and not
  43. by another, unless we start talking about Unix-specific features
  44. (pipes, sockets, select()).
  45. >>
  46. >>>The standard I/O package is one such creation. It is designed to provide fast
  47. >>>buffered I/O, mostly for text situations.
  48. >>               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  49. >>???
  50. >
  51. >I use the stdio routines almost exclusively myself. Even for binary files. 
  52. >That doesn't make the above statement wrong. Your statements seem to imply 
  53. >that all systems are buffered at the OS level, so who cares what I/O 
  54. >approach you use. You're not going to see any difference in I/O speed no 
  55. >matter what. While this may be true for your particular situation, it's by 
  56. >no means universal. The usefulness of buffering is in working sequentially
  57. >with a file. This makes buffered I/O well suited for text files. That 
  58. >doesn't mean you can't or won't read a binary file sequentially or that it 
  59. >sucks for use with binary files. Which, judging from what you've written,
  60. >is exactly what you'll think I meant. 
  61.  
  62. You meant that stdio routines are somehow more suitable for text I/O,
  63. which doesn't happen to be true.  The stdio buffering is a red herring, 
  64. because it can be always disabled, if it isn't needed.
  65.  
  66. >>>For a lot of applications, using the
  67. >>>standard I/O package is simpler and more effective than using simple low-level
  68. >>>output. The essential feature is its use of automatic buffering. Buffered 
  69. >>>I/O means reading and writing data in large chunks from a file to an array
  70. >>>and back. Reading and writing data in large chunks greatly speeds up the I/O
  71. >>>operations, while storing in an array allows access to the individual bytes.
  72. >>>The advantages of this approach should be apparent.
  73. >>
  74. >>Except that you can read and write data in large chunks using stdio 
  75. >>routines.  The impact of the extra level of buffering is insignificant.
  76. >
  77. >Isn't the above blurb talking about the stdio routines? Once again, your 
  78. >reference to an extra level of buffering is not applicable to all systems. 
  79. ???  Do you mean there are systems where stdio doesn't perform its own
  80. buffering?
  81.  
  82. >If you're really referring to the low-level routines, then your statement 
  83. >is not always true.
  84.  
  85. I was referring to the stdio routines.  In the (rare) cases where the
  86. stdio buffering has a non-negligible impact, it can be simply disabled.
  87.  
  88. >>>When fopen() is used, several things happen. The file, of course, is opened.
  89. >>>Second, an external character array is created to act as a buffer. The 
  90. >>><stdio.h> file has this buffer set to a size of 512 bytes.
  91. >>
  92. >>To a default size of _minimum_ 256 bytes.  I'm typing this text on a
  93. >>system which uses a default buffer size of 8192 bytes.  The size of the
  94. >>buffer can be controlled by the programmer via the setvbuf function.
  95. >>
  96. >That's a big default buffer. How many disk accesses does it take to fill 
  97. >that thing?
  98.  
  99. If its size matches the size of the lower level buffer, the number of
  100. disk accesses is irrelevant, because the same number will be performed
  101. even if I ask read() to read 1 byte from the file.  The implementor is
  102. the one who actually _knows_ what is the optimal size.  And the implementor
  103. decided that the st_blksize field of the stat struct should be set to
  104. (pure coincidence :-) 8192.  For the Unix illiterate:
  105.  
  106.     ues5:~/tmp 201> grep st_blksize /usr/include/sys/stat.h
  107.     ulong_t st_blksize;  /* Optimal blocksize for file system i/o ops */
  108.  
  109. >I seriously doubt you would want a buffer that big when reading 
  110. >a file randomly. But, maybe your system is so fast you can afford that kind 
  111. >of waste. In my <stdio.h> file, the constant BUFSIZ is 512. 
  112.  
  113. Aha, I see where the magical value 512 came from.  The systems I'm working
  114. on have BUFSIZ set to values between 1024 (mostly BSD-based Unices) and 
  115. 32768 (OpenVMS).  So, your claim that <stdio.h> sets the size of the
  116. buffer to 512 bytes is bogus.  BTW, even 512 is a humongous size for a
  117. single density 8" floppy disk, which needs to read four sectors to fill
  118. such a buffer.
  119.  
  120. >You say that 
  121. >you can control the size of the buffer using setvbuf(). That's true, but it
  122. >doesn't change the default buffer size as you seem to imply.
  123.  
  124. Huh???  Where did I imply that setvbuf changes the _default_ buffer size.
  125.  
  126. >If I do the following:
  127. >
  128. >FILE *input, *output;
  129. >
  130. >input = fopen("myfile.in", "r+b");
  131. >setvbuf(input, NULL, _IOFBF, 1024);
  132. >
  133. >output = fopen("myfile.out", "w+b");
  134. >
  135. >do you think output points to a buffer of the size set by setvbuf? 
  136.  
  137. Which should I think such a stupidity?
  138.  
  139. >I don't think this is what you meant. 
  140.  
  141. And I _definitely_ didn't mean that.
  142.  
  143. >>>Technically, in DOS, all I/O is buffered since DOS itself uses buffers
  144. >>>for disk I/O. 
  145. >>
  146. >>The same applies to other operating systems (e.g. Unix) except that they
  147. >>do a much better job at buffering I/O than MSDOS.
  148. >
  149. >Some versions of UNIX don't do any buffering at all.
  150.  
  151. Care to mention a few examples?  Unix has had block devices for as long
  152. as I can remember.  And I have yet to see a disk which can perform a
  153. 1-byte write.  But wait, you can always read a full sector in memory,
  154. change the byte and write back the sector immediately.  Voila, Unix without
  155. buffering.  Performance?  Who cares about performance???
  156.  
  157. >>>Hope this clarifies things.
  158. >>
  159. >>So many inaccurate statements don't clarify things.  On the contrary.
  160. >
  161. >The shit you have to up with when trying to be helpful. 
  162.  
  163. And when you don't succeed.  Spreading misinformation doesn't help anybody.
  164.  
  165. Dan
  166. --
  167. Dan Pop
  168. CERN, CN Division
  169. Email: danpop@mail.cern.ch 
  170. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  171.